package com.crm.jpaandthymeleaf.service.impl;

import com.crm.jpaandthymeleaf.entity.SysRole;
import com.crm.jpaandthymeleaf.entity.SysUser;
import com.crm.jpaandthymeleaf.repository.SysUserRepository;
import com.crm.jpaandthymeleaf.service.ISysUserService;
import com.crm.jpaandthymeleaf.service.RedisService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户业务实现层
 * @author 周敬
 * @version 1.0
 * @packageName com.crm.jpaandthymeleaf.service.impl
 * @fileName ISysUserServiceImpl
 * @createTime 2021/12/15-20:55-星期三
 * @lastModify 2021/12/15-20:55-星期三
 */
@Service
public class ISysUserServiceImpl implements ISysUserService {
    @Resource
    private SysUserRepository sysUserRepository;
    @Resource
    private RedisService redisService;

    @Override
    public SysUser login(String usrName, String usrPassword) {
        List<SysUser> sysUser = sysUserRepository.findByUsrNameAndUsrPassword(usrName, usrPassword);
        if(sysUser.size()<1){
            return null;
        }
        return sysUserRepository.findByUsrNameAndUsrPassword(usrName,usrPassword).get(0);
    }

    @Override
    public void saveSysUser(SysUser user) {
        sysUserRepository.save(user);
    }

    @Override
    public void deleteSysUser(Long usrId) {
        sysUserRepository.deleteById(usrId);
    }

    @Override
    @Cacheable(value = "SysUser",keyGenerator = "keyGenerator")
    public SysUser getSysUser(Long usrId) {
           SysUser user = sysUserRepository.findById(usrId).get();
            redisService.set(usrId.toString(),user);
        return user;
    }

    @Override
    public Page<SysUser> findSysUsers(String usrName, Long roleId, Pageable pageable) {
        Specification<SysUser> specification = new Specification<SysUser>() {
            @Override
            public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(usrName!=null && !usrName.equals("")){
                    predicates.add(criteriaBuilder.like(root.get("usrName"),"%"+usrName+"%"));
                }
                if(roleId!=null && roleId.longValue()!=0L){
                    SysRole role = new SysRole();
                    role.setRoleId(roleId);
                    predicates.add(criteriaBuilder.equal(root.get("role"),role));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return sysUserRepository.findAll(specification,pageable);
    }

    /**
     * 根据用户名查询用户
     *
     * @param usrName
     * @return
     */
    @Override
    public SysUser getUserByUserName(String usrName) {
        return sysUserRepository.findSysUserByUsrName(usrName);
    }

}
